{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" width=\"500 px\" align=\"center\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## _*Qiskit Aqua: Using Grover Search for 3SAT problems*_\n",
    "\n",
    "This notebook is based on an official notebook by Qiskit team, available at https://github.com/qiskit/qiskit-tutorial under the [Apache License 2.0](https://github.com/Qiskit/qiskit-tutorial/blob/master/LICENSE) license. \n",
    "\n",
    "This notebook demonstrates how to use the `Qiskit Aqua` library Grover algorithm and process the result.\n",
    "\n",
    "Your **TASK** is to execute every step of this notebook while learning to use qiskit-aqua and also strengthening your knowledge of the Grover search algorithm."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pylab\n",
    "from qiskit.aqua import run_algorithm\n",
    "from qiskit.aqua.input import get_input_instance\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as draw\n",
    "from qiskit.tools.visualization import plot_histogram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have a SAT problem to which we want to find solutions using Grover and SAT oracle combination. The SAT problem is specified in the  [DIMACS CNF format](https://www.satcompetition.org/2009/format-benchmarks2009.html). We read one of\n",
    "the sample cnf files to load the problem.\n",
    "\n",
    "Try the code below for both sample files `3sat3-5.cnf` and `3sat2-3.cnf`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c This is an example DIMACS 3-sat file with 3 satisfying solutions: 1 -2 3 0, -1 -2 -3 0, 1 2 -3 0\n",
      "p cnf 3 5\n",
      "-1 -2 -3 0\n",
      "1 -2 3 0\n",
      "1 2 -3 0\n",
      "1 -2 -3 0\n",
      "-1 2 3 0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open('3sat3-5.cnf', 'r') as f:\n",
    "    sat_cnf = f.read()\n",
    "print(sat_cnf)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to run an algorithm we need to create a configuration dictionary with the parameters for the algorithm and any other dependent objects it requires. So we first define a dictionary for the algorithm. We name GROVER as the algorithm and as it has no further parameters we are done. GROVER needs an oracle so we configure one. Here we use the SAT oracle which will allow us to solve an optimization SAT problem by searching solution space. We configure the oracle with the problem we loaded above. We then combine the dictionaries into the final single params dictionary that is passed to run_algorithm."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, -2, 3]\n"
     ]
    }
   ],
   "source": [
    "algorithm_cfg = {\n",
    "    'name': 'Grover'\n",
    "}\n",
    "\n",
    "oracle_cfg = {\n",
    "    'name': 'SAT',\n",
    "    'cnf': sat_cnf\n",
    "}\n",
    "\n",
    "params = {\n",
    "    'problem': {'name': 'search', 'random_seed': 50},\n",
    "    'algorithm': algorithm_cfg,\n",
    "    'oracle': oracle_cfg,\n",
    "    'backend': {'name': 'qasm_simulator'}\n",
    "}\n",
    "\n",
    "result = run_algorithm(params)\n",
    "print(result['result'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As seen above, a satisfying solution to the specified sample SAT problem is obtained, with the absolute values indicating the variable indices, and the signs the `True/False` assignments, similar to the DIMACS format.\n",
    "\n",
    "A measurements result is also returned where it can be seen, below in the plot, that result\\['result'\\] was the highest probability. But the other solutions were very close in probability too."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAf4AAAEMCAYAAAAlNdQ3AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2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4mIeG5/Juj4lnW5QW7kp6fzJSA8kvspwfOkV5vZGwQfus8RDOUoJpjlLusoN+XWBnUE7iWYUOrPwMvhWfBoglsZRwDHEXR+yzZRzm0o2Zsb5EB+OuPfR2Z2FDCR4D7qoQQftEvdfUFGA0sC5ZadcjW3mBneGj9pcCow2917ZDC8VlFu2Sub81PhT4JwKE5Hd9+W6ViSTbllpxzPre6hNdcTTI5ybaZjShbllr2yKT8VfhHJSmbWE/jcc/CJbsote2VDfir8IiIiEaLhfCIiIhGiwi8iIhIhKvwiIiIRosIvIiISISr8IiIiEaLCLyIiEiH/HxIXZgGDUYcoAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 576x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pylab.rcParams['figure.figsize'] = (8, 4)\n",
    "plot_histogram(result['measurements'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try the following command `draw(result['circuit'])` \n",
    "\n",
    "Which should produce a figure like:\n",
    "\n",
    "![grover](grover_output.png)\n",
    "\n",
    "If it does not and an error is showw instead it is due to a bug in the drawing mechanism the `cnx` gates may not be visible yet, this issue will surely be fixed in later versions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#draw(result['circuit'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above figure shows the circuit that was run for Grover. This circuit was returned from the algorithm for the above visualization which was generated using qiskit.tools.visualization functionality."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
